package com.glodon.paas.document.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glodon.paas.account.api.bean.User;
import com.glodon.paas.consts.StringConst;
import com.glodon.paas.document.api.bean.File;
import com.glodon.paas.document.service.FileService;
import com.glodon.paas.document.web.util.SecurityUtils;

@Controller
@RequestMapping("/revision")
public class FileRevisionController {

    public static final Logger     LOGGER = LoggerFactory.getLogger(FileRevisionController.class);

    @Autowired
    private FileService        fileService;
    
    @Autowired
    private ActivityPublisher  activityPublisher;

    public void setFileService(FileService service) {
        fileService = service;
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void delReversion(@PathVariable("id") String id, ModelMap model) {
        User user = SecurityUtils.getCurrentUser();
        File file = fileService.removeRevision(user.getId(), id);
        model.addAttribute(StringConst.RESULT, StringConst.SUCCESS);
        // Activity
        activityPublisher.delReversion(user.getId(), user.getDisplayName(), file.getId(), file.getName(), true);
    }
}
